import { randomInt } from "crypto";

const words = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const nums = ['0', '1', '2', '3', '4', '5', '6','7', '8', '9']

const WORD_LENG = words.length
const NUMS_LENG = nums.length

function randomAWord() {
  return words[randomInt(WORD_LENG)]
}

function randomANum() {
  return nums[randomInt(NUMS_LENG)]
}

/**
 * 生成n位验证码
 */
const generateVerify = (n = 1) => {
  if (!n || isNaN(n)) return 'invalid length'
  let arr = new Array(n)
  for (let i = 0; i < n; i++) {
    arr[i] = Math.random() > 0.5? randomAWord() : randomANum()
  }
  return arr.join('').toLocaleUpperCase()
}

export function generateOrderCode() {
  var timestamp = new Date().getTime();
  var randomNum = Math.random().toString().slice(2, 8);
  return timestamp + randomNum;
}

export {
  generateVerify
}